/*
 create by sys on 2016/11/22.
 */

define(["require",'config/appregister',"business/Companyinfor/service/htttp",
"business/services/regionService",
 "business/myResume/services/jobIntensionComp",
"business/services/dateComp","business/services/filter","jquery","business/workSkill/directive/startLevel"],function(require,app,httpServices){
    app.controller('sendWokrCtrl',function($scope,$timeout,$state,auth){
        auth.isCompanyLogin();
        $scope.isDetailPage = true;
        $scope.isMainpage = false;
        /*发布职位*/
       	$scope.stateGo = function(state){
			if(!state)return ;
			$state.go(state);
		};
	
        $scope.majorConf = {
            top:"",
            second:"",
            detail:"",
            isShow:false
        };//专业配置
         $scope.majorConf2 = {
            top:"",
            second:"",
            detail:"",
            isShow:false
        };//专业
        $scope.showMajor=function(a){
        	$scope.majorConf.isShow=true;
        	
        }
        $scope.showMajor2=function(a){
        	$scope.majorConf2.isShow=true;
        }
  		   //职能
        $scope.jobInJobConf = {
            top:"",
            detail:"",
            showValue:"",
            isShow:false,
            maxSelect:1
        };
        $scope.showWoekSkill=function(){
        	 $scope.jobInJobConf.isShow=true;
        }
		
        $scope.workExpression = {
            startValue:"",
            endValue:"",
            isShow:true
        };
        $scope.tags2 = [//ngInputTab自己写方法

        ];
        $scope.sendTag=[];
        $scope.controlLength = function(str){
            var l = str.length;
            var blen = 0;
            for(var i=0; i<l; i++) {
                if (str.charCodeAt(i) > 255) {
                    blen ++;
                }
                blen ++;
            }
            if(blen == 32){
                var maxLength = $scope.tagsWork.length;
            }
            if(blen > 32){
                $scope.tagsWork.length = maxLength;
            }
        }
        $scope.saveTagsname=function(a){
        	if($scope.tags2.length<10){
        		if(a!=undefined && a != ""){
		                if($scope.tags2.indexOf(a) != -1){
		                    alert("福利不能相同哦")
		                }else{
		                    if(a.length>6){
		                        alert("每个标签不得超过6个字")
		
		                    }else{
		                        $scope.tags2.push(a);
		                        $scope.sendTag.push(a);
		                        $scope.tagsWork=""
		                    }
		
		                }
		                $scope.olda = a;
		            }

        	}else{
        		alert("不得超出10个福利")
        	}
            
        }
        $scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
            $scope.tagsWork=""
        }

            $scope.showWorkBody=4;
            function getLanguage(){
                var req ={
                    type:"GET",
                    uri:"getLanguage",
                    data:{}
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                        /*console.log(JSON.stringify(response))*/
                        $scope.languaOne = response.data;
                    })
                });
            }
            getLanguage();
            function getLanguaOnesize(){
                var req ={
                    type:"GET",
                    uri:"getLanguageGrade",
                    data:{}
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){

                        $scope.languaOneSize = response.data;
                    })
                });
            }
            getLanguaOnesize();
            function getMap(){
                var req ={
                    type:"GET",
                    uri:"getProvinceList",
                    data:{}
                };
                httpServices.getMap(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.provinceCon = response.data;
                    })
                });

            }
            getMap();
            function getworkYear(){//工作年限
                var req ={
                    type:"GET",
                    uri:"getJobExperience",
                    data:{}
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.workYear = response.data;
                        /*console.log(response)*/
                    })
                });
            }
            getworkYear();
            function getWorkStu(){//最低学历
                var req ={
                    type:"GET",
                    uri:"getJobEducation",
                    data:{}
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.Education = response.data;
                    })
                });
            }
            getWorkStu();
            function getproperty(){//所有工作性质
                var req ={
                    type:"GET",
                    uri:"getJobNature",
                    data:{}
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                        /*console.log(JSON.stringify(response))*/
                        $scope.workproperty = response.data;
                    })
                });
            }
            getproperty();
            function getConpanyInfo(){
            	var req ={
                    type:"GET",
                    uri:"getBaseInfoByUser",
                    data:{}
                };
                httpServices.getCompany(req).success(function(result){
                    $scope.$apply(function(){
                        $scope.compayId = result.data.companyId
                    })
                });
            	
            	
            	
              /*  $.ajax({
                    type: "GET",
                    dataType: "json",
                    url: "http://"+allUrl+":8080/elink_user/api/company/getBaseInfoByUser",
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success: function(result) {
                        $scope.compayId = result.data.companyId
                        
                    },
                    error: function(jqXHR) {
                        alert("发生错误：" + jqXHR.status)
                    }
                });*/

            }
            getConpanyInfo();
            function getCommoney(){
                var req ={
                    type:"GET",
                    uri:"getSalaryCount",
                    data:{}
                };
                httpServices.getMoney(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.monye=response.data;
                    })
                });
            }
            getCommoney();

        $scope.moneyCon=$scope.choiLanTwo = $scope.choiLanTwogra = $scope.choiLanonegra= $scope.choiLanone = "请选择";
        $scope.languaArr=[];
        $scope.savelanOne=function(item){
            if(item.name == $scope.choiLanTwo){
                alert("语言2已经选择了哦")
            }else{
                $scope.langOneId = item.languageId;//语言1的id；
                $scope.choiLanone = item.name;
            }

        }
        $scope.savelanTwo=function(item){
            if(item.name ==  $scope.choiLanone){
                alert("语言1已经选择了哦")
            }else{
                $scope.langTwoId = item.languageId;//语言2的id；
                $scope.choiLanTwo = item.name;
            }
        }
        $scope.savelanOnegra=function(item){
            $scope.langOnegraId = item.gradeId;//语言1的等级id；
            $scope.choiLanonegra = item.name;
            /*console.log($scope.langOnegraId)*/
            /*$scope.languaArr.push(obj);*/
        }
        $scope.savelanTwogra=function(item){
            $scope.langTwograId = item.gradeId;//语言2Id
            $scope.choiLanTwogra = item.name;
        }
        $scope.saveworMoney=function(item){
            $scope.moneyId = item.srId;
            $scope.moneyCon=item.srName;
        }
        $scope.choiceAddress = "请选择";
        $scope.savePriceId = function(item){
            $scope.areaName = $scope.regionName = "请选择"
            $scope.provinceId = item.provinceId;//省级地质ID
            $scope.choiceAddress = item.provinceName;
            var req ={
                type:"GET",
                uri:"getRegionListInProvince",
                data:{
                    provinceId:$scope.provinceId
                }
            };
            httpServices.getMap(req).success(function(response){
                $scope.$apply(function(){
                    $scope.Mycity = response.data;

                })
            });

        }
        $scope.areaName = $scope.regionName = "请选择"
        $scope.saveCItyId=function(item){
            $scope.areaName = "请选择"
            $scope.CItyId = item.regionId;
            $scope.regionName = item.regionName;
            var req ={
                type:"GET",
                uri:"getAreaListInRegionId",
                data:{
                    regionId:$scope.CItyId
                }
            };
            httpServices.getMap(req).success(function(response){
                $scope.$apply(function(){
                    $scope.areaCon= response.data;
                    /*  console.log(JSON.stringify(response))*/
                })
            });
        }
        $scope.saveArea = function(item){
            $scope.areaId = item.areaId;
            $scope.areaName=item.areaName
        }
        $scope.workYearname ="请选择";
        $scope.saveworkYear=function(item){
            $scope.WorkYearId = item.dictId;//工作年限id
            $scope.workYearname =item.dictName;
        }
        $scope.workStu= "请选择"
        $scope.saveworStu=function(item){
            $scope.workStuId = item.dictId;//最低学历iD
            $scope.workStu = item.dictName;
        }
        /*限制textatea的字数*/
        $scope.checkText = function (textarea) {
            /*console.log(textarea.length)*/
            $scope.textareaCon = textarea;
            $scope.length = textarea.length;
            if (textarea.length > 4000) {
                textarea =textarea.substr(0,4000);
                alert("输入内容超出限制了哦！");
            }
        };
        $scope.positname=function(name){
            $scope.posiname = name;
        }
        $scope.needNum=function(num){
            $scope.PeoNum=num;
            
        }
     
        $scope.check1=function(item,$index){
            $scope.chececk = item.dictId;
            $scope.chec=true;
        }
        $scope.saveDeatailsAdd=function(a){
            $scope.DeatilsAddre = a;
        }
        $scope.saveDepar=function(deparment){
            $scope.deparment = deparment;
        }
        $scope.saveJobId=function(item){
            sessionStorage.JOBcompanyId = item.jobId;
        }


        $scope.previews=function(path){
            alert("请先发表职位")
        }
        $scope.confirmSend=function(){//确认发布
        	var today = new Date();//获得当前日期
		    var year = today.getFullYear();//获得年份
		    var month = today.getMonth() + 1;//此方法获得的月份是从0---11，所以要加1才是当前月份
		    var day = today.getDate();//获得当前日期
		    var startime =year+'-'+month+'-'+day  
        	$scope.marjor= [
        		{"jpId":"0","pdcId":$scope.majorConf.detail.pdcId},
        		{"jpId":"0","pdcId":$scope.majorConf2.detail.pdcId}
        	]
            if($scope.langOneId!=undefined && $scope.langOnegraId != undefined && $scope.langTwoId == undefined && $scope.langTwograId == undefined ){
                $scope.languaArr=[
                    {
                        "jlId":0,
                        "jobId":"1",
                        "languageId":$scope.langOneId,
                        "gradeId":$scope.langOnegraId
                    }
                ]
            }else if($scope.langOneId==undefined && $scope.langOnegraId == undefined
                    && $scope.langTwoId != undefined && $scope.langTwograId != undefined){
                     $scope.languaArr=[
                   {
                        "jlId":0,
                        "jobId":"1",
                        "languageId": $scope.langTwoId,
                        "gradeId":  $scope.langTwograId
                    }

                ]
            }else {
                $scope.languaArr=[
                    {
                        "jlId":0,
                        "jobId":"1",
                        "languageId":$scope.langOneId,
                        "gradeId":$scope.langOnegraId
                    }, {
                        "jlId": 0,
                        "jobId": "1",
                        "languageId": $scope.langTwoId,
                        "gradeId": $scope.langTwograId
                    }
                ]

            }
        	var marjors = JSON.stringify($scope.marjor);
        	var langualge =JSON.stringify($scope.languaArr)
            if($scope.posiname == undefined){
                alert("请输入职位名称");
            }else if($scope.PeoNum == undefined){
                alert("请输入招聘人数")
            }else if($scope.choiceAddress=="请选择" | $scope.regionName=="请选择" | $scope.areaName=="请选择" ){
                alert("请选择工作地点");
            }else if($scope.chec==undefined){
                alert("请选择工作性质")
            }else if($scope.length==undefined || $scope.length==0){
            	 alert("请输入职位描述")
            }else if($scope.jobInJobConf.showValue==""){
            	alert("请输入职能")
            }else if($scope.moneyId == undefined){
            	alert("请选择薪资")
            }else{
          var req ={
                    type:"POST",
                    uri:"add",
                    data:{
                    	type:$scope.showWOrk,
                        jobName:$scope.posiname,
                        companyId:$scope.compayId,//$scope.compayId
                        jobNumber:$scope.PeoNum,
                        provinceId:$scope.provinceId,
                        regionId:$scope.CItyId,
                        areaId:$scope.areaId,
                        jobPlace:$scope.DeatilsAddre,
                        functionsDetailIds:$scope.jobInJobConf.detail[0].fdcId,//职能详情
                        experienceId:$scope.WorkYearId,//工作年限id
                        educationId:$scope.workStuId,//最低学历id
                        natureId:$scope.chececk,//工作性质
                        salaryId:$scope.moneyId,//薪资id
                        jobWelfare:$scope.sendTag.join(","),//福利
                        startTime:startime,
                        endTime:$scope.workExpression.startValue==""?"":$scope.workExpression.startValue.replace("/","-").replace("/","-"),
                        jobRequire:$scope.textareaCon,
                        department:$scope.deparment,
                        languageJsonStr:langualge,
                        profeJsonStr:marjors
                    }
              
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.areaCon= response.data;
                        if(response.msg == "success"){
                            alert("发表职位成功")
                            $scope.jobId = response.data;
                            $state.go("companyDetailsCtrl",{modeifyWorkId:$scope.jobId})
                        }
                        
                    })
                });
        
            	
          
            }

        }
  /*$scope.jobId*/
		$scope.previewWOrk=function(){
        	if($scope.jobId != undefined){
                $state.go("companyDetailsCtrl",{modeifyWorkId:$scope.jobId})
            }else{
                alert("请先发布职位")
        	}	
		}

    })
});
